﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using StockMatch.Service;
using StockMatch.Service.Entity;
using Titan;

namespace StockMatch.WebSite.Areas.Admin.Controllers
{
    /// <summary>
    /// 资讯的管理
    /// </summary>
    public class NewsController : AdminControllerBase
    {
        public ActionResult Index(int? page, string kw)
        {
            ViewBag.Menu = "News"; 
            ViewBag.Tag = "Index";

            int pageIndex = (page == null ? 1 : (int) page);

            #region 查询列表

            var query = new QueryExpression();
            query.EntityType = typeof (News);
            query.Select(News_.ALL);
            if (!string.IsNullOrEmpty(kw))
            {

            }
            query.OrderBys.Add(News_.NewsId.Desc);
            query.PageIndex = pageIndex;
            query.PageSize = 5;
            query.ReturnMatchedCount = true;
            var newss = new Newss();
            newss = ServiceManager.NewsService.GetNewses(query);

            #endregion

            ViewBag.TotalCount = newss.TotalCount;
            ViewBag.PageIndex = pageIndex;
            ViewBag.Keyword = kw;
            return View(newss);
        }

        #region 资讯：修改

        [HttpGet]
        public ActionResult Edit(string md,int? id,int page, string kw)
        {
            ViewBag.Menu = "News";
            ViewBag.Tag = "Edit";

            News news = new News();
            if (md == "update")
            {
                if (id == null || id == 0)
                {
                    throw new Exception("修改操作失败，未接收到要修改的数据");
                }
                news = ServiceManager.NewsService.GetNews((int)id);
            }

            ViewBag.PageIndex = page;
            ViewBag.Keyword = kw;
            ViewBag.Operation = md;
            return View(news);
        }


        [HttpPost]
        public ActionResult Edit(News news,string md,int page,string kw)
        {
            var rs = false;
            if (md == "add")
            {
                rs = ServiceManager.NewsService.Insert(news);
                page = 1;
                kw = null;
            }
            else if (md == "update")
            {
                var updatePropetry = new PropertyExpression[]
                {
                    News_.NewsTitle,
                    News_.NewsComtent
                };
                rs = ServiceManager.NewsService.Update(news, updatePropetry);
            }

            string message = (md == "update" ? "修改" : "添加");
            if (!rs)
            {
                return Content("<script>alert('资讯" + message + "失败');location.href='" + Url.Action("Edit", (new {id=news.NewsId,md=md,page = page, kw = kw })) + "'</script>", "text/html");
            }
            return Content("<script>alert('资讯" + message + "成功');location.href='" + Url.Action("Index", (new { page = page, kw = kw })) + "'</script>", "text/html");
        }

        #endregion

        #region 资讯：删除
        public ActionResult Delete(int id, int page,string kw)
        {
            if (id < 1)
            {
                throw new Exception("删除操作失败，ID为0");
            }
            var rs = ServiceManager.NewsService.Delete(id);
            //若删除失败，需要提示
            if (!rs)
            {
                return Content("<script>alert('资讯删除失败');location.href='" + Url.Action("Index", (new { page = page, kw = kw })) + "'</script>", "text/html");
            }
            return RedirectToAction("Index", new { page = page,kw = kw});
        }

        #endregion

        #region 资讯：详情

        public ActionResult Detail(int id)
        {
            if (id == 0)
            { 
                throw new Exception("Id为0");
            }
            var news = ServiceManager.NewsService.GetNews(id);
            return View(news);
        }

        #endregion

    }
}
